package com.sxt.demo.djiapplication.dao

import android.content.Context
import com.lzy.okhttputils.OkHttpUtils
import com.lzy.okhttputils.callback.FileCallback
import com.lzy.okhttputils.callback.StringCallback
import com.sxt.demo.djiapplication.common.ServerStatus
import com.sxt.demo.djiapplication.common.UrlConstants
import com.sxt.demo.djiapplication.entity.flightdata.FlightData
import com.sxt.demo.djiapplication.entity.flightdata.param.FlightDataAddParam
import com.sxt.demo.djiapplication.entity.flightdata.param.FlightDataAddResultData
import com.sxt.demo.djiapplication.entity.flightdata.param.FlightDataRemoveResultData
import com.sxt.demo.djiapplication.utils.DbManager
import com.sxt.demo.djiapplication.utils.GsonBinder
import com.sxt.demo.djiapplication.utils.ToastUtils
import okhttp3.Call
import okhttp3.Response
import java.io.File


@Suppress("unused")
class FlightDataService(val context: Context) {

    fun add(data: FlightData): Boolean {
        return try {
            DbManager.getDaoSession().flightDataDao.insertOrReplace(data)
            true
        } catch (e: Exception) {
            false
        }
    }

    fun queryAll(): List<FlightData> {
        return DbManager.getDaoSession().flightDataDao.loadAll()
    }


    fun deleteById(id: Long): Boolean {
        return try {
            DbManager.getDaoSession().flightDataDao.deleteByKey(id)
            true
        } catch (e: Exception) {
            false
        }
    }

    fun clear(): Boolean {
        return try {
            DbManager.getDaoSession().flightDataDao.deleteAll()
            true
        } catch (e: Exception) {
            false
        }
    }

    fun addFlightData(data: FlightDataAddParam) {
        OkHttpUtils.post(UrlConstants.FLIGHT_DATA)
            .tag(this)
            .upJson(GsonBinder.toJsonStr(data))
            .execute(object : StringCallback() {
                override fun onSuccess(result: String, call: Call, response: Response) {
                    val rs = GsonBinder.toObj(result, FlightDataAddResultData::class.java)
                    if (rs.resultStatus != ServerStatus.SUCCESS.code) {
                        ToastUtils.show(context, "数据错误: 有飞行数据保存失败")
                    }
                }

                override fun onError(call: Call?, response: Response?, e: Exception?) {
                    ToastUtils.show(context, "服务器错误: 有飞行数据保存失败")
                }
            })
    }

    fun getFlightDataExcelFile(fileCallback: ((File?) -> Unit)?) {
        OkHttpUtils.get(UrlConstants.EXPORT_EXCEL)
            .tag(this)
            .execute(object :
                FileCallback(
                    "${context.filesDir.absolutePath}/tmp_files/",
                    "飞行数据记录${System.currentTimeMillis()}.xlsx"
                ) {
                //文件下载时，需要指定下载的文件目录和文件名
                override fun onSuccess(file: File?, call: Call?, response: Response?) {
                    // file 即为文件数据，文件保存在指定目录
                    fileCallback?.invoke(file)
                }

                override fun downloadProgress(
                    currentSize: Long,
                    totalSize: Long,
                    progress: Float,
                    networkSpeed: Long
                ) {
                    //这里回调下载进度(该回调在主线程,可以直接更新ui)
                }
            })
    }

    fun removeAllData() {
        OkHttpUtils.delete(UrlConstants.FLIGHT_DATA)
            .tag(this)
            .execute(object : StringCallback() {
                override fun onSuccess(result: String?, call: Call?, response: Response?) {
                    val rs = GsonBinder.toObj(result, FlightDataRemoveResultData::class.java)
                    if (rs.resultStatus != ServerStatus.SUCCESS.code) {
                        ToastUtils.show(context, "旧数据清除失败")
                    } else {
                        ToastUtils.show(context, "旧数据清除成功")
                    }
                }
            })
    }

}